var tests = [   "test_dbmapping_shouldCheckPermissionManagement",
                "test_dbmapping_shouldAddAndRemoveCities",
                "test_dbmapping_shouldAddAndRemoveCurrencies",
                "test_dbmapping_shouldAddAndRemoveClubWithAllFunctionality"];

var test_dbmapping_shouldCheckPermissionManagement = function() {
    var oldSizeRoot = root.usermgr.permissiongroups.count();

    var p1 = new Permission("demo_permission_1");
    var p2 = new Permission("demo_permission_2");
    var p3 = new Permission("demo_permission_3");
    var p4 = new Permission("demo_permission_4");
    var p5 = new Permission("demo_permission_5");
    var p6 = new Permission("demo_permission_6");

    var pg1 = new PermissionGroup("pg_demo_1");
    var pg2 = new PermissionGroup("pg_demo_2");
    var pg3 = new PermissionGroup("pg_demo_3");

    var auth1 = new Authorization(p1);
    var auth2 = new Authorization(p2);
    var auth3 = new Authorization(p3);
    var auth4 = new Authorization(p4);
    var auth5 = new Authorization(p5);
    var auth6 = new Authorization(p6);

    assertTrue(pg1.authorizations.add(auth1));
    assertTrue(pg1.authorizations.add(auth2));

    assertTrue(pg2.authorizations.add(auth3));
    assertTrue(pg2.authorizations.add(auth4));
    assertTrue(pg2.authorizations.add(auth5));

    assertTrue(pg3.authorizations.add(auth6));

    assertTrue(root.usermgr.permissiongroups.add(pg1));
    assertTrue(root.usermgr.permissiongroups.add(pg2));
    assertTrue(root.usermgr.permissiongroups.add(pg3));

    assertNotNull(Permission.getById(p1._id));
    assertNotNull(Permission.getById(p2._id));
    assertNotNull(Permission.getById(p3._id));
    assertNotNull(Permission.getById(p4._id));
    assertNotNull(Permission.getById(p5._id));
    assertNotNull(Permission.getById(p6._id));

    root.usermgr.permissiongroups.removeChild(pg1);
    root.usermgr.permissiongroups.removeChild(pg2);
    root.usermgr.permissiongroups.removeChild(pg3);

    pg1.authorizations.removeChild(auth1);
    pg1.authorizations.removeChild(auth2);

    pg2.authorizations.removeChild(auth3);
    pg2.authorizations.removeChild(auth4);
    pg2.authorizations.removeChild(auth5);

    pg3.authorizations.removeChild(auth6);

    auth1.remove();
    auth2.remove();
    auth3.remove();
    auth4.remove();
    auth5.remove();
    auth6.remove();

    p1.remove();
    p2.remove();
    p3.remove();
    p4.remove();
    p5.remove();
    p6.remove();

    pg1.remove();
    pg2.remove();
    pg3.remove();

    assertEqual(root.usermgr.permissiongroups.count(), oldSizeRoot);
    res.commit();
};

var test_dbmapping_shouldAddAndRemoveCitiesAndUsersAndContact = function() {
    var oldCount = root.addresses.countries.count();
    var sysAdminCount = root.sysadmins.count();
    var addressesCount = root.addresses.contacts.count();

    var helgoland = new Country("HGL", "Helgoland");
    assertTrue(root.addresses.countries.add(helgoland));

    assertEqual(0, helgoland.cities.count());

    var helgotown = new City("HG-123-17", "Helgotown");
    assertTrue(helgoland.cities.add(helgotown));

    assertEqual(1, helgoland.cities.count());
    assertEqual("HG-123-17", helgoland.cities.get(0).zip);
    assertEqual("Helgotown",  helgoland.cities.get(0).name);

    // Create a user
    var city =  root.addresses.countries.get("HGL").cities.get("HG-123-17");
    assertNotNull(city);

    //(street, street2, city, phone, fax, email, url)
    var contact = new Contact("street1", "street2", city, "+123456", "+78910", "demo@example.com", "http://test.example.com");

    var mustermann = new User("DEMOUSER", "DEMOUSER", "Thomas", "Muster", "Weltmeister", "Musterland AG", false, true, contact, null, false);
    assertTrue(root.sysadmins.add(mustermann));
    assertEqual(sysAdminCount + 1, root.sysadmins.count());

    assertEqual("demouser", mustermann.username);
    assertEqual("DEMOUSER", mustermann.password);
    assertEqual("Thomas", mustermann.firstname);
    assertEqual("Muster", mustermann.lastname);
    assertEqual("Weltmeister", mustermann.title);
    assertEqual("Musterland AG", mustermann.company);
    assertFalse(mustermann.iscompany);
    assertTrue(mustermann.active);
    assertEqual(contact.street, mustermann.contact.street);
    assertNull(mustermann.permissiongroup);
    assertFalse(mustermann.isprepaid);
    
    assertTrue(contact.isPersistent());
    assertTrue(mustermann.isPersistent());
    assertTrue(city.isPersistent());
    assertTrue(helgoland.isPersistent());

    // Cleanup the users
    contact.remove();
    root.sysadmins.removeChild(mustermann);
    mustermann.remove();

    // Remove the city
    helgoland.removeChild(helgotown);
    helgotown.remove();
    assertEqual(0, helgoland.cities.count());
    assertEqual(oldCount + 1, root.addresses.countries.count());
    root.addresses.countries.removeChild(helgoland);
    helgoland.remove();

    assertEqual(oldCount, root.addresses.countries.count());
    assertEqual(sysAdminCount, root.sysadmins.count());
    assertEqual(addressesCount, root.addresses.contacts.count());
    res.commit();
};

var test_dbmapping_shouldAddAndRemoveCurrencies = function() {
    var oldCount = root.currencies.count();

    var cur = new Currency("DEMO CURRENCY", "XYZ", 1.1234);
    assertTrue(root.currencies.add(cur));
    
    var currency = root.currencies.get(root.currencies.count() - 1);
    assertEqual(currency.name, "DEMO CURRENCY");
    assertEqual(currency.code, "XYZ");
    assertEqual(currency.eurvalue, 1.1234);
    assertNotEqual(oldCount, root.currencies.count());

    root.currencies.removeChild(cur);
    cur.remove();

    assertEqual(oldCount, root.currencies.count());
    res.commit();
};

var test_dbmapping_shouldAddAndRemoveClubWithAllFunctionality = function() {
    var clubCount      = root.clubs.count();
    var countryCount   = root.addresses.countries.count();
    var addressesCount = root.addresses.contacts.count();
    var currencyCount  = root.currencies.count();

    var helgoland = new Country("XXX", "Helgoland");
    assertTrue(root.addresses.countries.add(helgoland));

    assertEqual(0, helgoland.cities.count());
    var helgotown = new City("XX-123-17", "Helgotown");
    assertTrue(helgoland.cities.add(helgotown));

    assertEqual(1, helgoland.cities.count());
    assertEqual("XX-123-17", helgoland.cities.get(0).zip);
    assertEqual("Helgotown",  helgoland.cities.get(0).name);

    var city =  root.addresses.countries.get("XXX").cities.get("XX-123-17");
    assertNotNull(city);

    var contact = new Contact("street1", "street2", city, "+123456", "+78910", "demo@example.com", "http://test.example.com");

    var cur = new Currency("DEMO CURRENCY", "ASD", 1.1234);
    assertTrue(root.currencies.add(cur));

    var democlub = new Club("democlubtest", "aliasdemoclubtest", contact, cur, "Europe/Vienna", true, 10, 20, true);
    assertTrue(root.clubs.add(democlub));
    assertTrue(democlub.isPersistent());

    assertEqual("democlubtest", democlub.name);
    assertEqual("aliasdemoclubtest", democlub.alias);
    assertEqual(cur, democlub.currency);
    assertEqual("Europe/Vienna", democlub.timezone);
    assertTrue(democlub.showuserinfo);
    assertEqual(20, democlub.reservationperiod);
    assertEqual(10, democlub.cancelationperiod);
    assertTrue(democlub.active);

    // User
    var user = new User("xhorst", "12345", "Horst", "Haider", "Dr.", "TC Hausmening", true, true, contact, null, true);
    assertTrue(democlub.users.add(user));

    // Usergroups
	var usergroup1 = new UserGroup("XXX-Stammkunde", "desc-1", 0.3);
	var usergroup2 = new UserGroup("XXX-Senior", "desc-2", 0.1);
    assertTrue(democlub.usergroups.add(usergroup1));
    assertTrue(democlub.usergroups.add(usergroup2));
    assertTrue(usergroup1.isPersistent());
    assertTrue(usergroup2.isPersistent());
    assertEqual(usergroup1.name, "XXX-Stammkunde");
    assertEqual(usergroup2.name, "XXX-Senior");
    assertEqual(usergroup1.description, "desc-1");
    assertEqual(usergroup2.description, "desc-2");

    // Locations and Courts
	var location = new Location("Tennishalle", "tennishalle", "desc-location", contact, 1, true);
	assertTrue(democlub.locations.add(location));
    assertTrue(location.isPersistent());
    assertEqual("Tennishalle", location.name);
    assertEqual("tennishalle", location.alias);
    assertEqual("desc-location", location.description);
    assertEqual(contact, location.contact);
    assertEqual(1, location.sortorder);
    assertTrue(location.active);

	var court = new Court("Platz 1", "platz1", "desc-court", 1, true);
	assertTrue(location.courts.add(court));
    assertTrue(court.isPersistent());
    assertEqual("Platz 1", court.name);
    assertEqual("platz1", court.alias);
    assertEqual("desc-court", court.description);
    assertEqual(1, court.sortorder);
    assertTrue(court.active);


    var timeslot    = new Timeslot(tenez.Date.getTime(0,00), tenez.Date.getTime(24,00), 60, new Date(2009, 0, 1, 0, 0, 0), null, "1111111", 18);
    court.timeslots.add(timeslot);

    var transaction = new Transaction("Testtransaction", 18, 0.1);
    user.transactions.add(transaction);
    assertTrue(transaction.isPersistent());
    assertEqual(user, transaction.user);

    var reservation = new Reservation(new Date(2009, 11, 9, 9, 0, 0), new Date(2009, 11, 9, 10, 0, 0));
    transaction.reservation = reservation;
    reservation.transaction = transaction;

    assertTrue(court.timeslots.add(timeslot));
    assertTrue(timeslot.reservations.add(reservation));
    assertTrue(timeslot.isPersistent());
    assertTrue(reservation.isPersistent());
    assertEqual(transaction.reservation._id, reservation._id);
    assertEqual(reservation.transaction._id, transaction._id);

    // Remove all objects
    assertEqual(1, timeslot.reservations.count());
    timeslot.reservations.removeChild(reservation);
    reservation.remove();
    assertEqual(0, timeslot.reservations.count());

    assertEqual(1, court.timeslots.count());
    court.timeslots.removeChild(timeslot);
    timeslot.remove();
    assertEqual(0, court.timeslots.count());    

    assertEqual(1, user.transactions.count());
    user.transactions.removeChild(transaction);
    transaction.remove();
    assertEqual(0, user.transactions.count());

    assertEqual(1, location.courts.count());
    location.removeChild(court);
    court.remove();
    assertEqual(0, location.courts.count());

    assertEqual(1, democlub.locations.count());
    democlub.locations.removeChild(location);
    location.remove();
    assertEqual(0, democlub.locations.count());

    assertEqual(2, democlub.usergroups.count());
    democlub.usergroups.removeChild(usergroup1);
    democlub.usergroups.removeChild(usergroup2);
    usergroup1.remove();
    usergroup2.remove();
    assertEqual(0, democlub.usergroups.count());

    assertEqual(1, democlub.users.count());
    democlub.users.removeChild(user);
    user.remove();
    assertEqual(0, democlub.users.count());

    root.clubs.removeChild(democlub);
    democlub.remove();

    root.addresses.contacts.removeChild(contact);
    contact.remove();

    helgoland.removeChild(helgotown);
    root.addresses.countries.removeChild(helgoland);
    helgotown.remove();
    helgoland.remove();

    root.currencies.removeChild(cur);
    cur.remove();

    assertEqual(clubCount, root.clubs.count());
    assertEqual(countryCount, root.addresses.countries.count());
    assertEqual(addressesCount, root.addresses.contacts.count());
    assertEqual(currencyCount, root.currencies.count());
    res.commit();
};
